package com.hrt.manage.address.city.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.base.BaseController;
import com.hrt.bean.Area;
import com.hrt.bean.City;
import com.hrt.bean.Province;
import com.hrt.manage.address.area.service.AreaService;
import com.hrt.manage.address.city.service.CityService;
@Controller
@RequestMapping(value = "/manage/province/city")
public class CityController extends BaseController<City>{
	@Autowired CityService cityService;
	@Autowired AreaService areaService;
	/**
	 * 
	 * @param pageIndex
	 * 
	 * @param pageSize
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/queryByPage")
	@ResponseBody
	public Map queryByPage(
			@RequestParam(value = "provincecode", required = false) String provincecode,
			@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			HttpServletRequest request) {
		Map resultMap = new HashMap();

		if (pageSize == null || "".equals(pageSize)) {
			pageSize = 30;
		}
		if (pageIndex == null || "".equals(pageIndex)) {
			pageIndex = 1;
		}
		setPageIndex(pageIndex);

		setPageSize(pageSize);
		list = cityService.getByProvinceCode(provincecode);

		setTotalCount(new Long(list.size()));
		String sql = "select city from City city where city.provincecode='"+provincecode+"' and isdelete=0";

		List<City> resultList = cityService.queryByPage(sql,
				getPageIndex(), getPageSize());
		setList(resultList);

		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));

		page = this.getPage();

		resultMap.put("pageResult", page);
		return resultMap;
	}

	/**
	 * 
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/list")
	public ModelAndView list(@RequestParam(value = "provincecode", required = true) String provincecode,
			HttpServletRequest request) {
		ModelAndView view = new ModelAndView();

		list = cityService.getByProvinceCode(provincecode);
		setTotalCount(new Long(list.size()));
		this.setPageCount(this.computePageCount(this.getTotalCount(),this.getPageSize()));

		page = this.getPage();
		view.addObject("provinceCode", provincecode);
		view.addObject("pageResult", page);
		return view;

	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody Long[] delete(
			@RequestParam(value = "ids", required = true) String ids,
			HttpServletResponse response) throws IOException {
		Long[] idArr = null;
		ModelAndView view = new ModelAndView();

		if (ids != null) {
			if (ids.indexOf(",") != -1) {
				String[] idStrs = ids.split(",");
				idArr = new Long[idStrs.length];
				for (int idx = 0; idx < idStrs.length; idx++) {
					idArr[idx] = Long.parseLong(idStrs[idx]);
				}
			} else {
				idArr = new Long[] { Long.parseLong(ids) };
			}

		}
		List<Area> areaList = null;
		List<Long> areaIds= new ArrayList<Long>();
		for(int i=0;i<idArr.length;i++){
			City city=cityService.getCityById(idArr[i]);
			if(city!=null){
				areaList = new ArrayList<Area>();
				String citycode = city.getCode();
				areaList = areaService.getAreaByCityCode(citycode);
				if(areaList!=null&& !areaList.isEmpty()){
					for(int j=0;j<areaList.size();j++){
						Area area = areaList.get(j);
						areaIds.add(area.getId());
					}
				}
			}
		}
		
		try {
			areaService.deleteById(areaIds.toArray(new Long[areaIds.size()]));
			cityService.deleteById(idArr);

			return idArr;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public void save(@ModelAttribute("cityInfor") City city,
			HttpServletRequest request, HttpServletResponse response) {
		System.out.println("save");
		try {
			cityService.saveCity(city);
			response.sendRedirect("list.do?provincecode="+city.getProvincecode());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "/show")
	@ResponseBody
	public Map show(@RequestParam(value = "id", required = true) Long cityId,
			HttpServletRequest request, HttpServletResponse response) {

		Map map = new HashMap();
		if (cityId != null && !"".equals(cityId)) {
			City city = this.cityService.getCityById(cityId);
			if (city != null) {
				map = transBean2Map(city);
			}

		}
		return map;

	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public void update(@ModelAttribute("cityInfor") City city,
			HttpServletRequest request, HttpServletResponse response) {
		cityService.updateCity(city);
		try {
			response.sendRedirect("list.do?provincecode="+city.getProvincecode());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
